package com.jec.module;


import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.jec.Bootstrap;
import com.jec.module.sysmanage.service.UserService;
import com.jec.module.sysmonitor.dao.CardTypeRepository;
import com.jec.module.sysmonitor.entity.CardType;

import lombok.extern.log4j.Log4j;

@Service
@Log4j
public class DatabaseStart {
	
	@Autowired
	private CardTypeRepository cardTypeRepository;
		
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Resource
	private UserService userService;
	
    @PostConstruct
    public void postConstruct(){
    	
        if(Bootstrap.isNeedInitializeDatabase()) {
        	try {
        		initialize();
        	} catch(Throwable t) {
        		log.error("Initialize database error.", t);
        	}
        }
        
        // patch for add system log delete right
        userService.patch081029();
        
    }
    
    public void initialize() {
    	
    	initCardType();
    	
    	userService.initialize();
    	
    	runScript();
    	
    }
    
    private void runScript() {
    	
    	jdbcTemplate.execute("DROP TABLE IF EXISTS `zhwg_view_terminal_device`;");
    	jdbcTemplate.execute("CREATE VIEW `zhwg_view_terminal_device` AS select `zhwg_terminal_device`.`id` AS `id`,`zhwg_terminal_device`.`name` AS `name`,`zhwg_terminal_device`.`location` AS `location`,`zhwg_terminal_device`.`category` AS `category`,`zhwg_terminal_device`.`code` AS `code`,`zhwg_terminal_device`.`element_id` AS `element_id`,`zhwg_monitor_element`.`name` AS `element_name` from (`zhwg_terminal_device` left join `zhwg_monitor_element` on((`zhwg_terminal_device`.`element_id` = `zhwg_monitor_element`.`id`)));");

    	jdbcTemplate.execute("DROP TABLE IF EXISTS `zhwg_view_config_terminal_business`;");
    	jdbcTemplate.execute("CREATE VIEW `zhwg_view_config_terminal_business` AS select `a`.`id` AS `id`,`a`.`device_number` AS `device_number`,`b`.`name` AS `device_name`,`a`.`type` AS `type`,`a`.`members` AS `members`,`b`.`element_id` AS `element_id`,`c`.`key_type` AS `key_type`,`c`.`key_value` AS `key_value`,`c`.`code` AS `code`,`c`.`name` AS `name` from ((`zhwg_config_terminal_business` `a` left join `zhwg_terminal_device` `b` on((`a`.`device_number` = `b`.`code`))) left join `zhwg_config_terminal_key` `c` on((`a`.`key_config_id` = `c`.`id`))) ;");

    	
    	jdbcTemplate.execute("DROP TABLE IF EXISTS `zhwg_view_card`;");
    	jdbcTemplate.execute("CREATE VIEW `zhwg_view_card` AS select `zhwg_device_card`.`id` AS `id`,`zhwg_device_card`.`element_id` AS `element_id`,`zhwg_device_card`.`slot_number` AS `slot_number`,`zhwg_card_type`.`code` AS `code`,`zhwg_card_type`.`name` AS `name`,`zhwg_card_type`.`port_count` AS `port_count` from (`zhwg_device_card` left join `zhwg_card_type` on((`zhwg_device_card`.`type` = `zhwg_card_type`.`code`))) ;");
    
    	jdbcTemplate.execute("DROP TABLE IF EXISTS `zhwg_view_config_terminal_key`;");
    	jdbcTemplate.execute("CREATE VIEW `zhwg_view_config_terminal_key` AS select `a`.`id` AS `id`,`b`.`element_id` AS `element_id`,`a`.`key_type` AS `key_type`,`a`.`key_value` AS `key_value`,`a`.`device_number` AS `device_number`,`b`.`name` AS `device_name`,`a`.`business_type` AS `business_type`,`a`.`code` AS `code`,`a`.`name` AS `name`,`a`.`user_level` AS `user_level` from (`zhwg_config_terminal_key` `a` left join `zhwg_terminal_device` `b` on((`a`.`device_number` = `b`.`code`))) ;");
   	
    	jdbcTemplate.execute("DROP TABLE IF EXISTS `zhwg_view_config_vdr`;");
    	jdbcTemplate.execute("CREATE VIEW `zhwg_view_config_vdr` AS select `a`.`id` AS `id`,`a`.`number` AS `port_number`,`b`.`name` AS `device_name`,`a`.`enable` AS `enable`,`b`.`code` AS `number`,`b`.`element_id` AS `netunit` from (`zhwg_device_port` `a` left join `zhwg_terminal_device` `b` on((`a`.`device_id` = `b`.`id`))) ;");

    	jdbcTemplate.execute("DROP TABLE IF EXISTS `zhwg_view_log_operate`;");
    	jdbcTemplate.execute("CREATE VIEW `zhwg_view_log_operate` AS select `zhwg_log_operate`.`id` AS `id`,`zhwg_user`.`name` AS `operator`,`zhwg_user_resource`.`name` AS `module`,`zhwg_log_operate`.`description` AS `description`,`zhwg_log_operate`.`create_time` AS `create_time` from ((`zhwg_log_operate` left join `zhwg_user` on((`zhwg_log_operate`.`operater` = `zhwg_user`.`id`))) left join `zhwg_user_resource` on((`zhwg_user_resource`.`id` = `zhwg_log_operate`.`resource_id`))) order by `zhwg_log_operate`.`create_time` desc ;");
    	
    }
    
    
    //@Transactional(propagation = Propagation.REQUIRED)
    private void initCardType() {
    	
    	CardType ct = null;
    	
		ct = new CardType();
		ct.setCode(0);
		ct.setName("空槽位");
		ct.setPortCount(0);
		cardTypeRepository.save(ct);
    	
		ct = new CardType();
		ct.setCode(1);
		ct.setName("主控板");
		ct.setPortCount(1);
		cardTypeRepository.save(ct);
    	
		ct = new CardType();
		ct.setCode(2);
		ct.setName("数字用户板");
		ct.setPortCount(16);
		cardTypeRepository.save(ct);
		
		ct = new CardType();
		ct.setCode(3);
		ct.setName("模拟用户板");
		ct.setPortCount(16);
		cardTypeRepository.save(ct);
    	
		ct = new CardType();
		ct.setCode(5);
		//ct.setName("以太网板");
		ct.setName("数据交换单元");
		ct.setPortCount(10);
		cardTypeRepository.save(ct);
    	
		ct = new CardType();
		ct.setCode(6);
		ct.setName("数字中继板");
		ct.setPortCount(8);
		cardTypeRepository.save(ct);
    	
		ct = new CardType();
		ct.setCode(8);
		ct.setName("录音板");
		ct.setPortCount(1);
		cardTypeRepository.save(ct);
    	
		ct = new CardType();
		ct.setCode(10);
		//ct.setName("光传输板");
		ct.setName("业务传输单元");
		ct.setPortCount(12);
		cardTypeRepository.save(ct);
    	
		ct = new CardType();
		ct.setCode(12);
		ct.setName("综合接口板");
		ct.setPortCount(12);
		cardTypeRepository.save(ct);
		
		ct = new CardType();
		ct.setCode(13);
		ct.setName("内通管理单元");
		ct.setPortCount(0);
		cardTypeRepository.save(ct);
		
		ct = new CardType();
		ct.setCode(14);
		ct.setName("千兆接口单元");
		ct.setPortCount(0);
		cardTypeRepository.save(ct);
    	
    }
	
}
